package com.hou.Config;

import com.hou.services.QJob1;
import com.hou.services.QJob2;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
@Configuration
public class QuartzConfig {


    @Autowired
    Scheduler scheduler;//任务调度器
    /**
     * 初始注入 scheduler
     * @return
     * @throws SchedulerException
     * */
     @Bean
    public Scheduler scheduler() throws SchedulerException{
         SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory();
         return schedulerFactoryBean.getScheduler();
     }

    /**
     * 开始执行所有任务
     * @throws SchedulerException
     * */

    public void startJob() throws SchedulerException{
          startJob1(scheduler);
          startJob2(scheduler);
        scheduler.start();

    }

    private void startJob1(Scheduler scheduler)throws SchedulerException {
        // JobDetail 是具体Job实例，通过JobBuilder构建JobDetail实例，JobDetail是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(QJob1.class).withIdentity("job1","group1").build();
        //基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10* * * * * ?");//默认值，可改为从数据库获取
        // TriggerBuilder 构建表达式触发器实例CronTrigger，CronTrigger继承于Trigger
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job1","group1").withSchedule(cronScheduleBuilder).build();
        // 绑定JobDetail和Trigger，生成一个被scheduler调度的任务
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    private void startJob2(Scheduler scheduler)throws SchedulerException{
        JobDetail jobDetail = JobBuilder.newJob(QJob2.class).withIdentity("job2", "group2").build();
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");//默认值，可改为从数据库获取
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job2", "group2").withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);

    }
    /**
     * 修改某个任务的执行时间
     * 时间修改后立即生效，spring task定时任务做不到
     * @param name
     * @param group
     * @param time
     * @return
     * @throws SchedulerException
     */
      public boolean modifyHob(String name,String group,String time)throws SchedulerException{
          Date date = null;
          TriggerKey triggerKey = new TriggerKey(name,group);
          CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
          String oldTime = cronTrigger.getCronExpression();
          if(!oldTime.equalsIgnoreCase(time)){
              CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
              CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group).withSchedule(cronScheduleBuilder).build();
              date = scheduler.rescheduleJob(triggerKey, trigger);
          }


          return date != null;
      }
    /**
     * 获取Job信息
     * @param name
     * @param group
     * @return
     * @throws SchedulerException
     */

     public String getJobInfo(String name,String group)throws SchedulerException{
          TriggerKey triggerKey = new TriggerKey(name,group);
          CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
          return String.format("time:%s,state:%s",trigger.getCronExpression(), scheduler.getTriggerState(triggerKey).name());
     }

     /**
      * 暂停所有任务
      * @throws SchedulerException
      * */
     public void pauseAllJob()throws SchedulerException{
         scheduler.pauseAll();
     }

     /**
      * 暂停单个任务
      * */

     public void pauseJob(String name,String group)throws SchedulerException{
         JobKey jobKey = new JobKey(name, group);
         JobDetail jobDetail = scheduler.getJobDetail(jobKey);
         if(jobDetail == null){
           scheduler.pauseJob(jobKey);
         }
     }
     /**
      * 恢复某个任务
      * @param name
      * @param group
      * @throws SchedulerException
      **/
     public void resumeJob(String name,String group)throws SchedulerException{
         JobKey jobKey = new JobKey(name, group);
         JobDetail jobDetail = scheduler.getJobDetail(jobKey);
         if(jobDetail == null){
             scheduler.resumeJob(jobKey);
         }
     }
    /**
     * 恢复所有任务
     * @throws SchedulerException
     **/
     public void resumeAllJob()throws SchedulerException{
         scheduler.resumeAll();
     }

    /**
     * 删除某个任务
     *
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void deleteJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.deleteJob(jobKey);
    }

}
